package com.tengju.support.domain.model.rule;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;

import java.util.List;

@AllArgsConstructor
public enum ConditionType {

    MATH(1,"数学符号比较匹配",List.of(Operation.GT,Operation.LT,Operation.GE,Operation.LE,Operation.EQ, Operation.NEQ,
            Operation.IN,Operation.NOT_IN,Operation.RIGHT_LIKE,Operation.LEFT_LIKE,Operation.IS_EMPTY,Operation.IS_NOT_EMPTY),
            "com.tengju.support.domain.model.rule.MathCondition"),
    TIME(2,"时间比较匹配",List.of(Operation.IS_TODAY,Operation.IS_NOT_TODAY),
            "com.tengju.support.domain.model.rule.TimeCondition"),

    ;

    private int val;
    private String desc;
    private List<Operation> operations;
    private String classPath;

    @JsonValue
    public int getVal() {
        return val;
    }

    public String getDesc() {
        return desc;
    }

    public List<Operation> getOperations() {
        return operations;
    }

    public String getClassPath() {
        return classPath;
    }

    @JsonCreator
    public static ConditionType parseByVal(Integer val) {
        if (val == null) {
            return null;
        }
        for (ConditionType type : values()) {
            if (type.val == val) {
                return type;
            }
        }
        return null;
    }

    public static ConditionType parseByOperation(Operation operation) {
        if (operation == null) {
            return null;
        }
        for (ConditionType type : values()) {
            if (type.operations.contains(operation)) {
                return type;
            }
        }
        return null;
    }
}
